package huzh.foundation.appserver;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

/**
 *<p>title:IplanetUtil</p>
 *<p>description:</p>
 * @since 2014-6-8
 * @author huzh
 * @version 1.0
 */
public class IplanetUtil extends AbstractAppServerService {

    /**IAS*/
    public static final String IAS = "iplanet";
    /**IAS_JNDI_FACTORY*/
    public static final String IAS_JNDI_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory";
    /**IAS_TX*/
    public static final String IAS_TX = "java:comp/UserTransaction";
    /**initialContext*/
    private static InitialContext initialContext = null;

    /**
     * @return
     * @throws NamingException
     */
    public InitialContext getInitialContext() throws NamingException{
        if (initialContext != null) {
            return initialContext;
        }

        initialContext = new InitialContext();
        return initialContext;
    }

    /**
     * @param dsJNDIName
     * @return
     * @throws NamingException
     */
    public DataSource getDataSource(String dsJNDIName) throws NamingException {
        Context ctx = getInitialContext();
        return getDataSource(ctx, dsJNDIName);
    }

    /**
     * @return
     * @throws NamingException
     */
    public UserTransaction getUserTx() throws NamingException {
        Context ctx = getInitialContext();
        return getUserTx(ctx, "java:comp/UserTransaction");
    }

    /**
     * @return
     * @throws NamingException
     */
    public Context getEnvContext() throws NamingException {
        Context ctx = getInitialContext();
        return getEnvContext(ctx);
    }

}
